package com.genreate.util.oss;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.File;

public class StsUpload {

    public static void main(String[] args) throws Exception {
        // 请填写步骤1.5生成的临时访问密钥AccessKey ID、AccessKey Secret和SecurityToken，而非RAM用户的身份凭证信息
        // 请注意区分STS服务获取的AccessKey ID是以STS开头
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "STS.NZhU7EoUN59F7N7wxh7csjm5D";
        String accessKeySecret = "DTz9AqGuhS73x7jJrMe3WPiFwkcQT9m5DEgmtL3CTWWB";
        // 填写获取的STS安全令牌（SecurityToken）。
        String stsToken= "CAIS5wJ1q6Ft5B2yfSjIr5ndHo3xgopvgvutNWiGk3g9O+xfhaiepjz2IHhMeHJhAeAZt/o2m25T7/sclrpiTIJff0vAYPB99Y9W9gWHao/M41scARWS0s/LI3OaLjKm9s6wCryLQLGwU/OpbE++2U0X6LDmdDKkckW4OJmS8/BOZcgWWQ/KClgjA8xNdDN/tOgQN3baKYz3UHjQj3HXEVBjtydllGp78t7f+MCH7QfEh1CItI1pro/qcJ+/dJsubtUtQ9azze1wMejjqHcJu0gXqvcm3fUcpG6a5IunbgMPs0ncAY396cZyKQJ1XK8+FpNfoeL0/f8C4bSLy9+mk0YSZL8MDHqEHd3+2qrYFeaxMcwwb6fr0YAOLkx40PMf2iFTghJzXGsiE7JJoe8nkpS6XR/wng62Kv27gxYiOkl06ttC9bBf4TuWsxTECZZhbXs3X9jm1DHCDScKcypy9wiH1sdy/zh4LTsPbxqAARPCB7aGdH3QCpBWHSFmkXIQ0vFoYNiGxtqoQtgsLoRJVuirfXlw7gD4mbQltdzrNiaLexo9qO1ya3VW2WEKxwKQjmJYkPrCQLqVsUFZiH2GfRI1c2vZ/UKSejYzJujH0JHOMuizAuH4rPhnuOOTH/7W+gZkeRjIIg5Utt2DP2tdIAA=";

        // 使用DefaultCredentialProvider方法直接设置AK和SK
//        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret, stsToken);
//        // 使用credentialsProvider初始化客户端
//        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
//        // 显式声明使用 V4 签名算法
//        clientBuilderConfiguration.setSignatureVersion(SignVersion.V2);
        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, stsToken);


        try {

            // 创建PutObjectRequest对象，将本地文件exampletest.txt上传至examplebucket
            PutObjectRequest putObjectRequest = new PutObjectRequest("xinyin", "123.png", new File("C:\\Users\\Administrator\\Pictures\\123.png"));

            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传文件
            PutObjectResult result = ossClient.putObject(putObjectRequest);
        } catch (OSSException oe) {
            oe.printStackTrace();
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            ce.printStackTrace();
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}